import com.vhly.util.DeviceInfo;

import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.midlet.MIDlet;

public final class SplashScreen extends Canvas
        implements Runnable {

    private int b;
    private int c;
    private int d;
    private int e;
    private int deviceScreenWidth;
    private int deviceScreenHeight;
    private String audioType;
    private MIDlet i;
    private Displayable j;
    public static boolean a = false;
    private static boolean k = true;
    private static boolean l = true;
    private static boolean m = true;
    private Image gameLogo;
    private Image soundAsk;
    private Thread p;
    private int q;
    private int r;
    private volatile Thread s;
    private long t;
    private static int u = 40;
    private Image scrollImage1;
    private Image scrollImage2;
    private Player soundPlayer;
    private Image background;
    private Image logos[];
    private static int A = 0;
    private int B[] = {
            35, 88, 140
    };
    private int C[] = {
            16, 14, 5, 1, -3, -2, 2
    };
    private int D[] = {
            18, 15, -2, 0
    };
    private int E[][];

    public SplashScreen(MIDlet midlet, Displayable displayable, Thread thread, int i1, int j1, int k1, int l1,
                        String s1) {
        audioType = "";
        q = -6;
        r = -7;
        t = 0L;
        scrollImage1 = null;
        scrollImage2 = null;
        background = null;
        int[] ai = new int[5];
        ai[0] = 16;
        ai[1] = 12;
        ai[2] = 2;
        ai[3] = -8;
        ai[4] = -12;
        E = (new int[][]{
                new int[]{
                        2, C[6], 0, C[6], 1, C[6], 500
                }, new int[]{
                        2, C[2], 0, C[2], 1, C[2], u
                }, new int[]{
                        2, C[1], 0, C[1], 1, C[1], u
                }, new int[]{
                        2, C[0], 0, C[0], 1, C[0], u, 2
                }, new int[]{
                        -1, D[0], -1, D[0], -1, D[0], u
                }, new int[]{
                        -1, D[0], 5, D[0], -1, D[0], u
                }, new int[]{
                        -1, D[0], 5, D[1], -1, D[0], u
                }, new int[]{
                        -1, D[0], 5, D[2], -1, D[0], u
                }, new int[]{
                        -1, D[0], 5, D[3], -1, D[0], u
                }, new int[]{
                        -1, D[0], 5, D[0], -1, D[0], u
                }, new int[]{
                        -1, D[0], 5, D[1], 5, D[0], u, 1
                }, new int[]{
                        -1, D[0], 5, D[2], 5, D[1], u
                }, new int[]{
                        5, D[0], 5, D[3], 5, D[2], u
                }, new int[]{
                        5, D[1], 5, D[0], 5, D[3], u
                }, new int[]{
                        5, D[2], 5, D[1], 5, D[0], u
                }, new int[]{
                        5, D[3], 5, D[2], 5, D[1], u
                }, new int[]{
                        5, D[0], 5, D[3], 5, D[2], u
                }, new int[]{
                        5, D[1], 5, D[0], 5, D[3], u
                }, new int[]{
                        5, D[2], 2, C[0], 5, D[0], u
                }, new int[]{
                        5, D[3], 2, C[1], 5, D[1], u
                }, new int[]{
                        5, D[0], 2, C[2], 5, D[2], u
                }, new int[]{
                        5, D[1], 2, C[3], 5, D[3], u
                }, new int[]{
                        5, D[2], 2, C[4], 5, D[0], u
                }, new int[]{
                        5, D[3], 2, C[5], 5, D[1], u
                }, new int[]{
                        3, C[0], 2, C[6], 5, D[2], u
                }, new int[]{
                        3, C[1], 2, C[6], 5, D[3], u
                }, new int[]{
                        3, C[2], 2, C[6], 5, D[0], u
                }, new int[]{
                        3, C[3], 2, C[6], 5, D[1], u
                }, new int[]{
                        3, C[4], 2, C[6], 5, D[2], u
                }, new int[]{
                        3, C[5], 2, C[6], 5, D[3], u
                }, new int[]{
                        4, C[0], 2, C[6], 3, C[6], u
                }, new int[]{
                        4, C[1], 2, C[6], 3, C[6], u
                }, new int[]{
                        4, C[2], 2, C[6], 3, C[6], u
                }, new int[]{
                        4, C[6], 2, C[6], 3, C[6], 1000
                }
        });
        setFullScreenMode(true);
        i = midlet;
        j = displayable;
        audioType = s1;
        q = -6;
        r = -7;
        p = thread;
        deviceScreenWidth = DeviceInfo.DEVICE_SCREEN_WIDTH;
        deviceScreenHeight = DeviceInfo.DEVICE_SCREEN_HEIGHT;
        System.out.println(" deviceScreenHeight=" + deviceScreenHeight);
        b();
        c();
        Display.getDisplay(i).setCurrent(this);
        b = gameLogo.getWidth();
        c = gameLogo.getHeight();
        d = background.getWidth();
        e = background.getHeight();
        s = new Thread(this);
        s.start();
    }

    public final void pointerPressed(int i1, int j1) {
        if (j1 > 290) {
            if (i1 < 70) {
                keyPressed(q);
                return;
            }
            if (i1 > 170)
                keyPressed(r);
        }
    }

    public final void pointerReleased(int i1, int j1) {
        if (j1 > 290) {
            if (i1 < 70) {
//                i1 = q;
//                i1 = this;
                q = i1;
                return;
            }
            if (i1 > 170) {
//                i1 = r;
//                i1 = this;
                r = i1;
            }
        }
    }

    public final void keyPressed(int i1) {
        if (m) {
            if (i1 == q) {
                a = true;
                k = true;
                m = false;
                return;
            }
            if (i1 == r) {
                a = false;
                k = true;
                m = false;
            }
        }
    }

    public final void keyReleased(int i1) {
    }

    public final void paint(Graphics g1) {
        g1.setClip(0, 0, deviceScreenWidth, deviceScreenHeight);
        g1.setColor(0);
        g1.fillRect(0, 0, deviceScreenWidth, deviceScreenHeight);
        g1.setClip(0, 0, deviceScreenWidth, deviceScreenHeight);
        if (l) {
            g1.setColor(0);
            g1.fillRect(0, 0, deviceScreenWidth, deviceScreenHeight);
            g1.translate((deviceScreenWidth - b) / 2, (deviceScreenHeight - c) / 2);
            g1.drawImage(gameLogo, 0, 0, 20);
            return;
        }
        if (m) {
            g1.setColor(0);
            g1.fillRect(0, 0, deviceScreenWidth, deviceScreenHeight);
            g1.translate((deviceScreenWidth - b) / 2, (deviceScreenHeight - c) / 2);
            g1.drawImage(gameLogo, 0, 0, 20);
            g1.translate(-(deviceScreenWidth - b) / 2, -(deviceScreenHeight - c) / 2);
            g1.drawImage(soundAsk, 0, deviceScreenHeight, 36);
            return;
        }
        if (k) {
            //            g1 = this;
            g1.setClip(0, 0, g1.getClipWidth(), g1.getClipHeight());
            g1.setColor(0);
            g1.fillRect(0, 0, g1.getClipWidth(), g1.getClipHeight());
            g1.translate((deviceScreenWidth - d) / 2, (deviceScreenHeight - e) / 2);
            g1.drawImage(background, 0, 0, 0);
            for (int i1 = 0; i1 < 3; i1++)
                if (E[A][i1 << 1] != -1) {
                    int j1 = B[i1];
                    if (E[A][i1 << 1] == 3)
                        j1 = B[0];
                    else if (E[A][i1 << 1] == 4)
                        j1 = B[2];
                    if (E[A][i1 << 1] == 5) {
                        int ai[] = {
                                0, -1, -1
                        };
                        if (E[A][(i1 << 1) + 1] == D[0])
                            g1.drawImage(scrollImage1, j1 + ai[i1], 73, 17);
                        else if (E[A][(i1 << 1) + 1] == D[1])
                            g1.drawImage(logos[E[A][i1 << 1]], j1, 100, 3);
                        else if (E[A][(i1 << 1) + 1] == D[2])
                            g1.drawImage(scrollImage2, j1 + ai[i1], 72, 17);
                    } else {
                        g1.drawImage(logos[E[A][i1 << 1]], j1, E[A][(i1 << 1) + 1] + 100, 3);
                    }
                }

            return;
        } else {
            g1.setColor(0);
            g1.fillRect(0, 0, g1.getClipWidth(), g1.getClipHeight());
            return;
        }
    }

    private synchronized void a() {
        if (s != null)
            s = null;
    }

    public final void run() {
        try {
            if (l) {
                repaint();
                serviceRepaints();
                Thread.sleep(2000L);
                l = false;
            }
            while (m) {
                repaint();
                serviceRepaints();
                Thread.sleep(200L);
            }
            while (k) {
                if (A == 0) {
                    SplashScreen v1 = this;
                    try {
                        if (a && v1.soundPlayer != null)
                            v1.soundPlayer.start();
                    }
                    catch (Exception _ex) {
                    }
                }
                repaint();
                serviceRepaints();
                Thread.sleep(E[A][6]);
                A++;
                long l1;
                if (t != 0L && (l1 = System.currentTimeMillis() - t) < (long) u)
                    Thread.sleep((long) u - l1);
                t = System.currentTimeMillis();
                if (A >= E.length)
                    k = false;
            }
        }
        catch (Exception _ex) {
            a();
            if (j != null) {
                Display.getDisplay(i).setCurrent(j);
                d();
                p.start();
                System.out.println("backCanvas");
            }
        } finally {
            a();
            if (j != null) {
                Display.getDisplay(i).setCurrent(j);
                d();
                p.start();
                System.out.println("backCanvas");
            }
        }
    }

    private void b() {
        try {
            gameLogo = Image.createImage("/logo/gameLogo.png");
            soundAsk = Image.createImage("/logo/soundAsk.png");
            background = Image.createImage("/logo/bg.png");
            logos = new Image[6];
            for (int i1 = 0; i1 < 6; i1++)
                logos[i1] = Image.createImage("/logo/logo" + i1 + ".png");

            scrollImage1 = Image.createImage("/logo/logo51.png");
            scrollImage2 = Image.createImage("/logo/logo52.png");
        }
        catch (Exception _ex) {
            System.out.println("there is a error when init");
        }
    }

    private void c() {
        try {
            if (audioType.equals("audio/amr"))
                soundPlayer = Manager.createPlayer(i.getClass().getResourceAsStream("/logo/sound1.amr"), "audio/amr");
            else if (audioType.equals("audio/midi"))
                soundPlayer = Manager.createPlayer(i.getClass().getResourceAsStream("/logo/sound1.mid"), "audio/midi");
            else if (audioType.equals("audio/x-wav"))
                soundPlayer = Manager.createPlayer(i.getClass().getResourceAsStream("/logo/sound1.wav"), "audio/x-wav");
            soundPlayer.realize();
            VolumeControl volumecontrol = (VolumeControl) soundPlayer.getControl("VolumeControl");
            if ((volumecontrol) != null)
                volumecontrol.setLevel(60);
            soundPlayer.prefetch();
            return;
        }
        catch (Exception exception) {
            return;
        }
    }

    private void d() {
        gameLogo = null;
        soundAsk = null;
        background = null;
        for (int i1 = 0; i1 < logos.length; i1++)
            logos[i1] = null;

        scrollImage1 = null;
        scrollImage2 = null;
        soundPlayer.close();
        soundPlayer = null;
    }

}